package demo06;

import java.util.Random;
import java.util.Scanner;

class Monster {
    private int health;
    private int attack;

    public Monster() {
        Random rand = new Random();
        health = rand.nextInt(20) + 10;
        attack = rand.nextInt(5) + 5;
    }

    public int getHealth() {
        return health;
    }

    public int getAttack() {
        return attack;
    }

    public void takeDamage(int damage) {
        health -= damage;
    }

    public boolean isAlive() {
        return health > 0;
    }
}

class BattleSystem {
    private Player player;
    private Monster monster;

    public BattleSystem(Player player, Monster monster) {
        this.player = player;
        this.monster = monster;
    }

    public boolean startBattle() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("A monster appears!");

        while (monster.isAlive() && player.getHealth() > 0) {
            System.out.println("\nPlayer Health: " + player.getHealth());
            System.out.println("Monster Health: " + monster.getHealth());
            System.out.println("Choose action:");
            System.out.println("1. Attack");
            System.out.println("2. Use Health Potion");
            System.out.print("> ");

            int choice = scanner.nextInt();

            if (choice == 1) {
                monster.takeDamage(player.getAttack());
                System.out.println("You hit the monster for " + player.getAttack() + " damage!");
            } else if (choice == 2) {
                player.useHealthPotion();
            } else {
                System.out.println("Invalid choice!");
                continue;
            }

            if (monster.isAlive()) {
                player.takeDamage(monster.getAttack());
                System.out.println("Monster hits you for " + monster.getAttack() + " damage!");
            }
        }

        if (player.getHealth() <= 0) {
            System.out.println("You died!");
            return false;
        } else {
            System.out.println("You defeated the monster!");
            return true;
        }
    }
}